# -*- coding: utf-8 -*-

from qiniu import Auth, put_file

from itpay.core.conf import QiniuConf


class QiniuClient:
    def __init__(self, access_key, secret_key, bucket_name):
        self.access_key = access_key
        self.secret_key = secret_key
        self.bucket_name = bucket_name
        self.q = Auth(access_key, secret_key)

    def get_token(self, key=None, timeout=120, policy=None):
        """key 上传到七牛后保存的文件名"""
        return self.q.upload_token(self.bucket_name, key, timeout, policy)

    def upload_file(self, token, key, localfile):
        ret, info = put_file(token, key, localfile)
        print(info)


client = QiniuClient(QiniuConf.access_key, QiniuConf.secret_key, QiniuConf.bucket_name)


def get_upload_token(key=None):
    return client.get_token(
        key, policy={"fsizeLimit": 50 * 1024, "mimeLimit": "image/*"}
    )
